package cn.ym.system.controller;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.basic.utils.PageList;
import cn.ym.system.domain.Role;
import cn.ym.system.query.RoleQuery;
import cn.ym.system.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：角色
 */
@RestController
@RequestMapping("/role")
@Api(value = "角色接口" ,description = "处理角色的所有请求")
public class RoleController {
    @Autowired
    private RoleService roleService;
    /**
     * 接口：添加或修改角色信息
     */
    @ApiOperation(value = "添加或修改角色信息")
    @PutMapping
    public AjaxResult save(@RequestBody Role role){

            if(role.getId()==null){
                roleService.add(role);
            }else {
                roleService.update(role);
            }
            return new AjaxResult();


    }

    /**
     *接口：删除单个角色信息
     */
    @ApiOperation(value = "删除单个角色信息")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

            roleService.delete(id);
            return new AjaxResult();

    }

    /**
     * 接口：查询所有角色信息
     */
    @ApiOperation(value = "查询所有角色信息")
    @GetMapping
    public List<Role> findAll(){
        return roleService.findAll();
    }

    /**
     * 接口：查询单个角色信息
     */
    @ApiOperation(value = "查询单个角色信息")
    @GetMapping("/{id}")
    public Role findOne(@PathVariable("id") Long id){
        return roleService.findOne(id);
    }


    /**
     * 接口：批量删除角色信息
     */
    @ApiOperation(value = "批量删除角色信息")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[]  ids){

            roleService.patchDel(ids);
            return new AjaxResult();

    }

    /**
     * 接口：分页查询 + 高级查询
     */
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    public PageList<Role> queryPage(@RequestBody RoleQuery roleQuery){
        return roleService.queryPage(roleQuery);
   }



}
